package com.dcdrake.testApp;

import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import classes.Event;
import java.util.ArrayList;

public class AddEventActivity extends Activity {
	final String debugTAG = "debug";
	public static final String PREFERENCE_FILENAME = "gamePrefs";
	EditText editName;
	EditText editDescription;
	EditText editLoc;
	DatePicker editDate;
	
	String eventName;
	int eventDay;
	int eventMonth;
	int eventYear;
	String eventLocation;
	String eventDescription;
	Date newDate;
	GregorianCalendar calendar;
	Event newEvent;
	ArrayList<Event> list;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.add);
	    list = ((myApp) this.getApplication()).getList();
	    editName = (EditText) findViewById(R.id.editText1);
	    editLoc = (EditText) findViewById(R.id.editText2);
	    editDate = (DatePicker) findViewById(R.id.datePicker1);
	    editDescription = (EditText) findViewById(R.id.editText3);
	    editDescription.setSelection(0);
	    
	}
	
	public void onClick(View view){
		switch(view.getId()){
		
		case R.id.save_button:
			eventName = editName.getText().toString();
			eventDay = editDate.getDayOfMonth();
			eventMonth = editDate.getMonth();
			eventYear = editDate.getYear();
			eventLocation = editLoc.getText().toString();
			eventDescription = editDescription.getText().toString();
			
			//Using deprecated Date class
			//newDate = new Date(eventYear, eventMonth, eventDay);
			//newEvent = new Event(eventName, newDate, eventLocation, eventDescription);
			
			//calendar = new GregorianCalendar(eventYear, eventMonth, eventDay);
			newEvent = new Event(eventName, eventYear, eventMonth, eventDay, eventLocation, eventDescription);
			list.add(newEvent);
			((myApp) this.getApplication()).setList(list);
			Log.v(debugTAG, "Made it here");
			//((myApp) this.getApplication()).setEvent(newEvent);
			
			 SharedPreferences gameSettings = getSharedPreferences(PREFERENCE_FILENAME, MODE_PRIVATE);    
				/*numTimesCompleted = gameSettings.getInt("numTimesCompleted", 0);
				shortestPath = gameSettings.getInt("shortestPath", 0);
				lowestEnergyConsumed = gameSettings.getFloat("lowestEnergyConsumed", 0);
				
				textView.setText("Distance Traveled: " + mz.pathLength +"\n" 
				+ "Energy Consumed: " + mz.energyConsumed + "\n"
				+"Number of Times Completed at this Difficulty: " + numTimesCompleted +"\n"
				+ "Shortest Path Found: " + shortestPath + "\n"
				+"Least Amount of Energy Consumed: " + lowestEnergyConsumed);
				
				numTimesCompleted += 1;
				if(shortestPath > mz.pathLength){
					shortestPath = mz.pathLength;
				}
				if(lowestEnergyConsumed > mz.energyConsumed){
					lowestEnergyConsumed = mz.energyConsumed;
				}*/
			 
				SharedPreferences.Editor prefEditor = gameSettings.edit(); 
				/*prefEditor.putInt("numTimesCompleted", numTimesCompleted);
				prefEditor.putInt("shortestPath", shortestPath);
				prefEditor.putFloat("lowestEnergyConsumed", lowestEnergyConsumed);*/
				prefEditor.commit(); 
				
			Intent addIntent = new Intent(view.getContext(), CampusEventsActivity.class);
        	startActivity(addIntent);
        	finish();
        	break;
		}
	}
}
	
